package com.da.boot.config;

import com.da.boot.interceptor.LoginInterceptor;
import com.da.boot.interceptor.RedisUrlCountInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author JokerDa.
 * @date 2021/1/23 14:38
 * Description:
 * 1、编写一个拦截器实现HandlerInterceptor接口
 * 2、拦截器注册到容器中（实现WebMvcConfigurer的addInterceptors）
 * 3、指定拦截规则【如果是拦截所有，静态资源也会被拦截】
 * @EnableWebMvc:全面接管 1、静态资源？视图解析器？欢迎页.....全部失效
 */
//@EnableWebMvc
@Configuration
public class AdminWebConfig implements WebMvcConfigurer {

    /**
     * Filter、Interceptor 几乎拥有相同的功能？
     * 1、Filter是Servlet定义的原生组件。好处，脱离Spring应用也能使用
     * 2、Interceptor是Spring定义的接口。可以使用Spring的自动装配等功能
     */
    @Autowired
    private RedisUrlCountInterceptor redisUrlCountInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor())
            .addPathPatterns("/**")
            .excludePathPatterns("/", "/login", "/css/**", "/fonts/**", "/images/**", "/js/**", "/aa/**"); //放行的请求

        registry.addInterceptor(redisUrlCountInterceptor)
            .addPathPatterns("/**")
            .excludePathPatterns("/", "/login", "/css/**", "/fonts/**", "/images/**", "/js/**", "/aa/**");
    }

    /**
     * 定义静态资源行为
     */
    //@Override
    //public void addResourceHandlers(ResourceHandlerRegistry registry) {
    //    /**
    //     * 访问  /aa/** 所有请求都去 classpath:/static/ 下面进行匹配
    //     */
    //    registry.addResourceHandler("/aa/**")
    //        .addResourceLocations("classpath:/static/");
    //}

    //@Bean
    //public WebMvcRegistrations webMvcRegistrations() {
    //    return new WebMvcRegistrations() {
    //        @Override
    //        public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
    //            return null;
    //        }
    //    };
    //}
}
